﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Controllers;
using InnovaApp.Layers.SecurityLayer.Models;
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common;

namespace InnovaApp.Layers.SecurityLayer.Controllers
{
    public class AccountActivationController : _Controllers_Base<AccountActivationModel>
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        /// <summary>
        /// Initializes data that might not be available when the constructor is called.
        /// </summary>
        /// <param name="requestContext">The HTTP context and route data.</param>
        protected override void Initialize(RequestContext requestContext)
        {
            base.eMethodBase = "InnovaApp.Layers.SecurityLayer.Controllers.RegisterController";
            base.specificCssCollection = new string[] { "_FormCommon.css", "Views/AccountCommon.css" };
            base.specificJSCollection = null;
            base.dynamicJSCollection = null;
            base.specificJqueryJSCollection = new string[] { "InnovacallJS/AccountActivation/AccountActivation.js" };
            base.externalCSS = null;
            base.Meta.Add(enMeta.Description, Resources.View_AccountActivation_Meta_Description);
            base.Meta.Add(enMeta.Keywords, Resources.View_AccountActivation_Meta_Keywords);
            base.Meta.Add(enMeta.Subject, Resources.View_AccountActivation_Meta_Subject);

            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        /// <summary>
        /// URL[Get]: /AccountActivation
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(string codeactivation, string email)
        {
            using (DataRepositories _dataContext = new DataRepositories())
            {
                base.InitView(
                    Resources.View_Register_PageTitle,
                    string.Empty,
                    specificCssCollection,
                    specificJSCollection,
                    dynamicJSCollection,
                    specificJqueryJSCollection,
                    jsVariables,
                    externalCSS,
                    Meta,
                    _dataContext,
                    false
                    );


                try
                {
                    string username = Membership.GetUserNameByEmail(email);

                    if (username != null)
                    {
                        MembershipUser user = Membership.GetUser(username);
                        if (user != null)
                        {
                            if ((user.Email + user.CreationDate).EncodeSHA1() == codeactivation.Trim())
                            {
                                if (user.IsApproved == true)
                                {
                                    ContentData.ActivationResult = Resources.View_AccountActivation_AccountAlreadyActive;
                                }
                                else
                                {
                                    user.IsApproved = true;
                                    Membership.UpdateUser(user);
                                    ContentData.ActivationResult = Resources.View_AccountActivation_Success;
                                }
                            }
                            else
                            {
                                ContentData.ActivationResult = Resources.View_AccountActivation_AccountActivationCodeNotValid;
                            }

                        }
                        else
                        {
                            ContentData.ActivationResult = Resources.View_AccountActivation_UnknownAccount;
                        }
                    }
                    else
                    {
                        ContentData.ActivationResult = Resources.View_AccountActivation_UnknownEMail;
                    }
                }
                catch (Exception ex)
                {
                    ContentData.ActivationResult = Resources.View_AccountActivation_Exeption;
                }

            }
            return View(ContentData);
        }

    }
}
